package com.wwwarehouse.carddesk.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.adapter.GroupDetailsAdapter;
import com.wwwarehouse.carddesk.bean.CardDeskGroupGetGroupBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.carddesk.fragment.teamfragment.checkdownclassfragemnt.CheckDownClassFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.checkupclassfragment.CheckUpClassFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.createrfragment.CreaterLookCreaterFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.createrfragment.CreaterLookManagerFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.createrfragment.CreaterLookWatcherFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.managerfragment.ManagerLookCreaterFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.managerfragment.ManagerLookManagerFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.managerfragment.ManagerLookWatcherFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.watcherfragment.WatcherLookCreaterFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.watcherfragment.WatcherLookManagerFragment;
import com.wwwarehouse.carddesk.fragment.teamfragment.watcherfragment.WatcherLookWatcherFragment;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * 团队框架
 * minifly
 * 2017:11:09 19:48
 *
 * @author minfily
 */

public class GroupDetailsFragment extends BaseFragment {

    private View mRootView;
    private GroupDetailsAdapter mAdapter;
    private List<CardDeskGroupGetGroupBean.ListBean> stableList;
    private GridView mGroupGridview;
    private StateLayout mGroupGridviewLayout;
    private int page;
    private int pageSize = 9;
    private static final int REQUEST_GETGROUPMESSAGE = 100005;
    private CardDeskGroupGetGroupBean bean;
    //上级 up 下级 down
    private static final String CREATOR = "creator",REGULATOR = "regulator",SUPERVISO = "supervisor" , UP = "up" , DOWN = "down";
    private String personName;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_group_details, container, false);
        return mRootView;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mGroupGridview = (GridView) mRootView.findViewById(R.id.group_gridview);
        mGroupGridviewLayout = (StateLayout) mRootView.findViewById(R.id.group_gridview_layout);

        stableList = new LinkedList<>();
        page = getArguments().getInt("page");
        personName = getArguments().getString("personName");
        getAllGroupMes();
    }

    @Override
    public void setTitle() {

    }

    @Override
    public void requestDatas() {

    }

    private void initData() {
        /**
         创造者看创造者 CreaterLookCreaterFragment
         创造者看管理者 CreaterLookManagerFragment
         创造者看监控者 CreaterLookWatcherFragment

         管理者看创造者 ManagerLookCreaterFragment
         管理者看管理者 ManagerLookManagerFragment
         管理者看监控者 ManagerLookWatcherFragment

         监控者看创造者 WatcherLookCreaterFragment
         监控者看管理者 WatcherLookManagerFragment
         监控者看监控者 WatcherLookWatcherFragment
         */

        mAdapter = new GroupDetailsAdapter(mActivity, stableList);
        mAdapter.setGridviewClicklistener(new GroupDetailsAdapter.GridviewClicklistener() {
            @Override
            public void click(CardDeskGroupGetGroupBean.ListBean bean) {
                /**
                 * buIdManagerType 当前用户在三角链中的身份（创造者creator,管理者regulator,监控者supervisor）
                 * cardManagerType  团队中每个小卡片角色（创造者creator,管理者regulator,监控者supervisor , 上级 up , 下级 down )
                 *
                 * GroupTransparentDataBean
                 */
                Bundle bundle = new Bundle();
                bundle.putParcelable("bean",bean);

                // 跳转页面一共是十一种情况  上级下级单独跳转 ，其他的九种三角链的跳转
                if(UP.equals(bean.getCardManagerType())){
                    CheckUpClassFragment createrLookManagerFragment = new CheckUpClassFragment();
                    createrLookManagerFragment.setArguments(bundle);
                    pushFragment(createrLookManagerFragment);
                }else if(DOWN.equals(bean.getCardManagerType())){
                    CheckDownClassFragment createrLookCreaterFragment = new CheckDownClassFragment();
                    createrLookCreaterFragment.setArguments(bundle);
                    pushFragment(createrLookCreaterFragment);
                }else{
                    switch (bean.getBuIdManagerType()){
                        case CREATOR:
                            if(CREATOR.equals(bean.getCardManagerType())){
                                CreaterLookCreaterFragment createrLookCreaterFragment = new CreaterLookCreaterFragment();
                                createrLookCreaterFragment.setArguments(bundle);
                                pushFragment(createrLookCreaterFragment);
                            }else if(REGULATOR.equals(bean.getCardManagerType())){
                                CreaterLookManagerFragment createrLookManagerFragment = new CreaterLookManagerFragment();
                                createrLookManagerFragment.setArguments(bundle);
                                pushFragment(createrLookManagerFragment);
                            }else if(SUPERVISO.equals(bean.getCardManagerType())){
                                CreaterLookWatcherFragment createrLookWatcherFragment = new CreaterLookWatcherFragment();
                                createrLookWatcherFragment.setArguments(bundle);
                                pushFragment(createrLookWatcherFragment);
                            }
                            break;
                        case REGULATOR:
                            if(CREATOR.equals(bean.getCardManagerType())){
                                ManagerLookCreaterFragment managerLookCreaterFragment = new ManagerLookCreaterFragment();
                                managerLookCreaterFragment.setArguments(bundle);
                                pushFragment(managerLookCreaterFragment);
                            }else if(REGULATOR.equals(bean.getCardManagerType())){
                                ManagerLookManagerFragment managerLookManagerFragment = new ManagerLookManagerFragment();
                                managerLookManagerFragment.setArguments(bundle);
                                pushFragment(managerLookManagerFragment);
                            }else if(SUPERVISO.equals(bean.getCardManagerType())){
                                ManagerLookWatcherFragment managerLookWatcherFragment = new ManagerLookWatcherFragment();
                                managerLookWatcherFragment.setArguments(bundle);
                                pushFragment(managerLookWatcherFragment);
                            }
                            break;
                        case SUPERVISO:
                            if(CREATOR.equals(bean.getCardManagerType())){
                                WatcherLookCreaterFragment watcherLookCreaterFragment = new WatcherLookCreaterFragment();
                                watcherLookCreaterFragment.setArguments(bundle);
                                pushFragment(watcherLookCreaterFragment);
                            }else if(REGULATOR.equals(bean.getCardManagerType())){
                                WatcherLookManagerFragment watcherLookManagerFragment = new WatcherLookManagerFragment();
                                watcherLookManagerFragment.setArguments(bundle);
                                pushFragment(watcherLookManagerFragment);
                            }else if(SUPERVISO.equals(bean.getCardManagerType())){
                                WatcherLookWatcherFragment watcherLookWatcherFragment = new WatcherLookWatcherFragment();
                                watcherLookWatcherFragment.setArguments(bundle);
                                pushFragment(watcherLookWatcherFragment);
                            }
                            break;
                        default:
                            break;
                    }
                }


            }
        });
        mGroupGridview.setAdapter(mAdapter);
    }


    public void getAllGroupMes() {
        Map<String, Object> map = new HashMap<>();
        /**
         * page		number
         * personName	姓名	string	非必填 搜索
         * size		number
         */
        map.put("page", page);
        if(TextUtils.isEmpty(personName)){
            map.put("personName", "");
        }else{
            map.put("personName", personName);
        }
        map.put("size", pageSize);

        mGroupGridviewLayout.showProgressView(null,true,false);

        NoHttpUtils.httpPost(CardDeskConstant.PATH_CARDDESK_GROUP_GETGROUP_MESSAGE, map, myOnresponse, REQUEST_GETGROUPMESSAGE);
    }

    NoHttpUtils.OnResponseListener myOnresponse = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            switch (what) {
                case REQUEST_GETGROUPMESSAGE:
                    if ("0".equals(commonClass.getCode())) {
                        bean = JSON.parseObject(commonClass.getData().toString(), CardDeskGroupGetGroupBean.class);
                        stableList = bean.getList();
                        initData();
                    } else {
                        mGroupGridviewLayout.showSystemView("" + commonClass.getMsg(), true);
                        mGroupGridviewLayout.setSystemListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                getAllGroupMes();
                            }
                        });
                    }

                    break;

                default:
                    break;
            }
        }

        @Override
        public void onFailed(String result, int what) {

        }

        @Override
        public void onFinish(int what) {
            mGroupGridviewLayout.showContentView();
        }
    };

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }
}
